package com.nan.translation.controller;


import com.alibaba.fastjson.JSONObject;
import com.nan.translation.annotation.UserLoginToken;
import com.nan.translation.model.LanguageResult;
import com.nan.translation.model.TranslationResult;
import com.nan.translation.model.User;
import com.nan.translation.service.LanguageService;
import com.nan.translation.service.TokenService;
import com.nan.translation.service.UserService;
import com.nan.translation.service.TranslateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.nan.translation.interceptor.AuthenticationInterceptor.ERROR401;

@CrossOrigin
@RestController
@RequestMapping(path="/")
public class TranslateController {
    private final UserService userService;
    private final TokenService tokenService;
    private final TranslateService translateService;
    private final LanguageService languageService;

    @Autowired
    public TranslateController(UserService login, TokenService token, TranslateService translate, LanguageService language) {
        this.userService = login;
        this.tokenService = token;
        this.translateService = translate;
        this.languageService = language;
    }

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public Object  Login(HttpServletRequest request){
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");

        JSONObject jsonObject=new JSONObject();
        User user = userService.findUserByName(name);
        if(user == null){
            jsonObject.put("errno", ERROR401);
        }
        else if(!user.getPassword().equals(pwd)){
            jsonObject.put("errno", ERROR401);
        }else {
            jsonObject.put("errno", "0");
            jsonObject.put("token", tokenService.getToken(user));
            jsonObject.put("user", user);
        }
        return jsonObject;
    }

    //test method
    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }

    @RequestMapping(path="/register", method = RequestMethod.POST)
    public boolean Register(HttpServletRequest request){
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        User user1 = new User(name, pwd);
        User user2 = userService.addUser(user1);
        return user1.equals(user2);
    }

    @UserLoginToken
    @RequestMapping(path="/translate")
    public  @ResponseBody
    TranslationResult translate(@RequestParam Map<String,Object> params){
        String to = "en";

        if(params.containsKey("to"))
            to = params.get("to").toString();

        return new TranslationResult(translateService.getTransResult(params.get("text").toString(), to));
    }

    @UserLoginToken
    @RequestMapping(path="/language")
    public  @ResponseBody
    LanguageResult language(@RequestParam Map<String,Object> params){
        return new LanguageResult(languageService.getLangResult(params.get("text").toString()));
    }


}
